/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.crab.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.service.CrudService;
import com.kaaction.modules.crab.entity.CouponDetail;
import com.kaaction.modules.crab.utils.KuaiDi100;
import com.kaaction.modules.crab.dao.CouponDao;
import com.kaaction.modules.crab.dao.CouponDetailDao;
import com.kaaction.modules.crab.dao.CrabProductDao;
import com.kaaction.modules.crab.dao.OrderDao;

/**
 * 兑换券查看Service
 * @author huanghaiming
 * @version 2017-07-19
 */
@Service
@Transactional(readOnly = true)
public class CouponDetailService extends CrudService<CouponDetailDao, CouponDetail> {
	
	@Autowired 
	OrderDao orderDao;
	
	@Autowired 
	CrabProductDao productDao;
	
	@Autowired 
	CouponDao couponDao;
	
	
	public CouponDetail get(String id) {
		return super.get(id);
	}
	
	public List<CouponDetail> findList(CouponDetail couponDetail) {
		return super.findList(couponDetail);
	}
	
	public Page<CouponDetail> findPage(Page<CouponDetail> page, CouponDetail couponDetail) {
		String  numStart = couponDetail.getNumStart();
		String  numEnd = couponDetail.getNumEnd();
		if(StringUtils.isNotBlank(numStart)){
			if(numStart.length()>4){
				couponDetail.setNumStartOne(numStart.substring(0, numStart.length()-4));
				couponDetail.setNumOne(numStart.substring(numStart.length()-4, numStart.length()));
			}else{
				couponDetail.setNumStartOne("99887766");
			}
		}
		if(StringUtils.isNotBlank(numEnd)){
			if(numEnd.length()>4){
				couponDetail.setNumEndTwo(numEnd.substring(0, numEnd.length()-4));
				couponDetail.setNumTwo(numEnd.substring(numEnd.length()-4, numEnd.length()));
			}else{
				couponDetail.setNumEndTwo("99887766");
			}
		}
		
		return super.findPage(page, couponDetail);
	}
	
	@Transactional(readOnly = false)
	public void save(CouponDetail couponDetail) {
		super.save(couponDetail);
	}
	
	@Transactional(readOnly = false)
	public void delete(CouponDetail couponDetail) {
		super.delete(couponDetail);
	}

	public List<CouponDetail> findCouponDetail(CouponDetail couponDetail) {
		String  numStart = couponDetail.getNumStart();
		String  numEnd = couponDetail.getNumEnd();
		if(StringUtils.isNotBlank(numStart)){
			if(numStart.length()>4){
				couponDetail.setNumStartOne(numStart.substring(0, numStart.length()-4));
				couponDetail.setNumOne(numStart.substring(numStart.length()-4, numStart.length()));
			}else{
				couponDetail.setNumStartOne("99887766");
			}
		}
		if(StringUtils.isNotBlank(numEnd)){
			if(numEnd.length()>4){
				couponDetail.setNumEndTwo(numEnd.substring(0, numEnd.length()-4));
				couponDetail.setNumTwo(numEnd.substring(numEnd.length()-4, numEnd.length()));
			}else{
				couponDetail.setNumEndTwo("99887766");
			}
		}
		return super.findList(couponDetail);
	}

	@Transactional(readOnly = false)
	public void updateBatchRemarks(CouponDetail couponDetail) {
		Map<String,Object> map = new HashMap<String,Object>();
		String numStart = couponDetail.getNumStart();
		String numEnd = couponDetail.getNumEnd();
		String numS = numStart.substring(0,numStart.length()-4);
		int ns = Integer.valueOf(numStart.substring(numStart.length()-4,numStart.length()));
		int ne = Integer.valueOf(numEnd.substring(numEnd.length()-4,numEnd.length()));
		
		List<String> list = Lists.newArrayList();
		for(int i=ns;i<=ne;i++){
			String str = numS+String.format("%4d", i).replace(" ", "0");
			list.add(str);
		}
		map.put("list", list);
		map.put("remarks", couponDetail.getRemarks());
		super.dao.updateBatchRemarks(map);
	}

	@Transactional(readOnly = false)
	public void updateBatchActivate(CouponDetail couponDetail) {
		
		Map<String,Object> map = new HashMap<String,Object>();
		String numStart = couponDetail.getNumStart();
		String numEnd = couponDetail.getNumEnd();
		String numS = numStart.substring(0,numStart.length()-4);
		int ns = Integer.valueOf(numStart.substring(numStart.length()-4,numStart.length()));
		int ne = Integer.valueOf(numEnd.substring(numEnd.length()-4,numEnd.length()));
		List<String> list = Lists.newArrayList();
		for(int i=ns;i<=ne;i++){
			String str = numS+String.format("%4d", i).replace(" ", "0");
			list.add(str);
		}
		map.put("list", list);
		map.put("activatedState", couponDetail.getActivatedState());
		super.dao.updateBatchActivate(map);
		
	}

	@Transactional(readOnly = false)
	public void udateCouponName(CouponDetail couponDetail) {
		super.dao.udateCouponName(couponDetail);
		couponDao.updateCoupon(couponDetail.getCouponId());
	}

	public Map<String,Object> selCoupon(String cardNum,String password) {
		return super.dao.selCoupon(cardNum,password);
	}

	public Map<String,Object> selCardNumMsg(String cardNum,String password) {
		Map<String,Object> map = super.dao.selCoupon(cardNum,password);
		if(map!=null&&map.size()>0){
			String useState  = (String) map.get("useState");
			if("0".equals(useState)){
				map.put("listRecommendProduct", productDao.selRecommendProduct());
				return map;
			}else if("1".equals(useState)){
					map.clear();
					map = orderDao.selCoupon(cardNum,password);
					if(map!=null&&map.size()>0){
						String state = (String) map.get("state");
						if("0".equals(state)){
							map.put("listRecommendProduct", productDao.selRecommendProduct());
							map.put("useState", "1");
							return map;
						}else if("1".equals(state)){
							Object expressNum = (String) map.get("expressNum");
							if(expressNum!=null){
								String  url = KuaiDi100.searchkuaiDiInfo("1949fc1aa47e87f1", "shunfeng", expressNum.toString());
								map.put("logisticsInformationUrl", url);
							}
							map.put("useState", "1");
							return map;
						}
					}else{
						return map;
					}
			}
		}
		
		 return map;
	}

	public Map<String,Object> selExchangeCouponProduct(String cardNum, String password) {
		
		Map<String,Object> map =  super.dao.selCoupon(cardNum,password);
		
		if(map!=null&&map.size()>0){
			String couponId = (String) map.get("couponId");
			map.put("listExchangeCouponProduct", productDao.selExchangeCouponProduct(couponId));
		}
		return map;
	}

	public int getEqual(String cardNum) {
		return super.dao.getEqual(cardNum);
	}

	@Transactional(readOnly = false)
	public void updateCoupon(CouponDetail couponDetail) {
		 couponDetail.preUpdate();
		 super.dao.updateCoupon(couponDetail);
	}

	public int selCnt(List<String> strList) {
		return  super.dao.selCnt(strList);
	}
	
}